package logic

import (
	"context"
	"errors"
	"user_srv/models"

	"user_srv/internal/svc"
	"user_srv/user"

	"github.com/zeromicro/go-zero/core/logx"
)

type DelPositionLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewDelPositionLogic(ctx context.Context, svcCtx *svc.ServiceContext) *DelPositionLogic {
	return &DelPositionLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *DelPositionLogic) DelPosition(in *user.DelPositionRequest) (*user.DelPositionResponse, error) {
	// todo: add your logic here and delete this line
	var p models.Position
	err := p.GetPosition(int(in.PositionId))
	if err != nil {
		logx.Error("删除失败:信息不存在", err)
		return nil, err
	}
	if p.UserId != in.UserId {
		logx.Error("删除失败:", errors.New("权限不足"))
		return nil, errors.New("权限不足")
	}

	err = p.DelPosition()
	if err != nil {
		logx.Error("删除失败:", err)
		return nil, err
	}

	return &user.DelPositionResponse{}, nil
}
